//
//  GuideViewController.m
//  ZoneTry
//
//  Created by youminuo on 16/5/27.
//  Copyright © 2016年 ZoneTry. All rights reserved.
//

#import "GuideViewController.h"

@interface GuideViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollerView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,strong) UIButton *enterButton;
@end

@implementation GuideViewController
#pragma mark --懒加载
- (UIScrollView *)scrollerView{
    if (!_scrollerView) {
        _scrollerView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _scrollerView.delegate = self;
        _scrollerView.pagingEnabled = YES;
        _scrollerView.bounces = NO;
        _scrollerView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollerView;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.frame = CGRectMake(_scrollerView.frame.size.width * 0.5, _scrollerView.frame.size.width - 100, 0, 0);
        _pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];//选中颜色
        _pageControl.pageIndicatorTintColor = [UIColor redColor];//默认颜色
    }
    return _pageControl;
}

- (UIButton *)enterButton{
    if (!_enterButton) {
        _enterButton = [[UIButton alloc]init];
        [_enterButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [_enterButton setTitle:@"立即体验" forState:UIControlStateNormal];
        [_enterButton sizeToFit];
    }
    return _enterButton;
}

#pragma mark --UI
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:_scrollerView];
    [self.view addSubview:_pageControl];
    [self setupUI];
    self.view.backgroundColor = [UIColor redColor];
}

- (void)setupUI{
    int count = 4;
    for (int i = 0; i < count; i++) {
        NSString *imgName = [NSString stringWithFormat:@"%d",i];
        UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
        img.frame = CGRectMake((CGFloat)i * _scrollerView.frame.size.width, 0, _scrollerView.frame.size.width, _scrollerView.frame.size.height);
        [_scrollerView addSubview:img];
        if (i == count -1) {
            [self setLastPage:img];
        }
    }
    _scrollerView.contentSize = CGSizeMake((CGFloat)count * _scrollerView.frame.size.width, 0);
    _pageControl.numberOfPages = count;
}

- (void)setLastPage:(UIView *)page{
    page.userInteractionEnabled = YES;
    _enterButton.center = CGPointMake(page.frame.size.width * 0.5, page.frame.size.height - 150);
    [page addSubview:_enterButton];
}

#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat page = _scrollerView.contentOffset.x / _scrollerView.frame.size.width;
    _pageControl.currentPage = (int)page;
}

@end
